home *** CD-ROM | disk | FTP | other *** search
- /**********************************************\
- * *
- * simutil.c = Utility operations for SIMPP: *
- * Simple IMage Processing Package. *
- * Copyright (c) 1987, Benjamin M. Dawson *
- * Edit Version: 1.2 : Jan-30-87 *
- * *
- \**********************************************/
-
- #include <stdio.h>
- #include "simpp.h"
-
- extern char *malloc();
-
- /* These flags are for open() to indicate read/write ability on a file.
- * The are defined for Berkeley UNIX, but may have to be changed for your
- * system.
- */
- #define READ_ONLY 00000
- #define WRITE_ONLY 01001
-
- /* clear_area = Clear an area , begining at x,y and of size dx,dy to
- * value z.
- */
- int clear_area(x,y,dx,dy,z)
- int x,y; /* Start of area to set */
- int dx,dy; /* Size of area to set */
- PIXEL z; /* Value to set area to */
- {
- register int i;
- register PIXEL *bp;
- PIXEL *buf;
-
- #ifdef CHECK
- if (check_area(x,y,dx,dy,"<clear_area>") == ERROR)
- return(ERROR);
- #endif
- /* Set up a buffer with size of dx */
- buf = bp = (PIXEL *)malloc(dx*sizeof(PIXEL));
- /* Set it to all z values */
- for (i = 0 ; i < dx ; i++) *bp++ = z;
- /* Clear lines */
- while (dy--) write_hline(x,y++,dx,buf);
-
- free(buf);
- return(OK);
- }
-
- /* ================================================================ */
-
- /* save_image = Save the image starting at x,y and of size dx,dy into the
- * filewith name name.
- */
- int save_image(x,y,dx,dy,name)
- int x,y; /* Start of area to save */
- int dx,dy; /* Size of area to save */
- char *name; /* File name to use for save */
- {
- int fd;
- short xx,yy,dxx,dyy; /* Shorts for header */
- PIXEL *buf, *bp;
- int wrsize;
-
- #ifdef CHECK
- if (check_area(x,y,dx,dy,"<save_image>") == ERROR)
- return(ERROR);
- #endif
-
- /* Copy integer values to shorts, so that the header is transportable
- between machines */
- xx = (short)x; yy = (short)y;
- dxx = (short)dx; dyy = (short)dy;
-
- /* Open file for writing */
- if ((fd = open(name,WRITE_ONLY,0777)) < 0) { /* Write only open */
- printf("<save_image> Can't open file %s !\n",name);
- perror("=");
- return(ERROR);
- }
-
- /* Write out header -- xx,yy, dxx,dyy */
- write(fd,&xx,sizeof(short));
- write(fd,&yy,sizeof(short));
- write(fd,&dxx,sizeof(short));
- write(fd,&dyy,sizeof(short));
-
-
- /* Allocate a buffer */
- bp = buf = (PIXEL *)malloc(dx*sizeof(PIXEL));
- /* Write out image data */
- wrsize = dx*sizeof(PIXEL);
- while (dy--) {
- read_hline(x,y++,dx,bp);
- if (write(fd,bp,wrsize) < wrsize) {
- printf("<save_image> File write error!\n");
- close(fd);
- return(ERROR);
- }
- }
-
- close(fd); /* Close file */
- free(buf); /* Free buffer */
- return(OK);
- }
-
- /* ================================================================ */
-
- /* read_image = Read an image from disk storage. If any of the
- * values x,y,dx,dy are < 0, then the corresponding value from the
- * file is used.
- */
- int read_image(x,y,dx,dy,name)
- int x,y; /* Start of area to read */
- int dx,dy; /* Size of area to read */
- char *name; /* File name to use for read */
- {
- int fd;
- short xx,yy,dxx,dyy; /* Shorts for header */
- PIXEL *buf, *bp;
- int rdsize;
-
- /* Open file for reading */
- if ((fd = open(name,READ_ONLY,0777)) < 0) { /* Open read only */
- printf("<read_image> Can't open file %s !\n",name);
- return(ERROR);
- }
-
- /* Read header -- xx,yy, dxx,dyy */
- read(fd,&xx,sizeof(short));
- read(fd,&yy,sizeof(short));
- read(fd,&dxx,sizeof(short));
- read(fd,&dyy,sizeof(short));
-
- /* Check if you should use these values or the argument values */
-
- if (x < 0 ) x = (int)xx;
- if (y < 0 ) y = (int)yy;
- if (dx < 0 ) dx = (int)dxx;
- if (dy < 0) dy = (int)dyy;
-
- /* See if you should truncate the size */
-
- if ((int)dxx < dx) dx = (int)dxx;
- if ((int)dyy < dy) dy = (int)dyy;
-
- /* See if it will run off the screen */
-
- if ((x + dx) > XSIZE) dx = XSIZE - x;
- if ((y + dy) > YSIZE) dy = YSIZE - y;
-
- /* Allocate a buffer */
- bp = buf = (PIXEL *)malloc(dx*sizeof(PIXEL));
-
- /* Read in image data */
- rdsize = (int)dxx*sizeof(PIXEL);
- while (dy--) {
- if (read(fd,bp,rdsize) < rdsize) {
- printf("<read_image> Image read error!\n");
- close(fd);
- return(ERROR);
- }
- write_hline(x,y++,dx,bp);
- }
-
- close(fd); /* Close file */
- free(buf); /* Free buffer */
- return(OK);
- }
-
- /* ================================================================ */
-
- /* draw_grid = Cover the screen with a grid of spacing dx,dy, and value z */
- VOID draw_grid(dx,dy,z)
- int dx,dy; /* Grid spacing */
- PIXEL z;
- {
- register int i;
- register PIXEL *bp;
- PIXEL *buf;
-
- /* Which direction, horizontal or vertical, is longer? */
- if (XSIZE > YSIZE) i = XSIZE;
- else i = YSIZE;
- /* Allocate a buffer of this size, and fill it with value z */
- buf = bp = (PIXEL *)malloc(i*sizeof(PIXEL));
- while (i--) *bp++ = z;
- /* Draw the lines */
- for (i = 0 ; i < YSIZE ; i += dy) write_hline(0,i,XSIZE,buf);
- for (i = 0 ; i < XSIZE ; i += dx) write_vline(i,0,YSIZE,buf);
-
- free(buf);
- }
-
- /* ================ End of simutil.c ================ */
-
- /* <-- FILE BREAK --> */